package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author taizhimin
 */
public class MainController implements Initializable {
    @FXML
    private MenuItem openFileBtn;
    @FXML
    private Stage primaryStage;
    @FXML
    private ListView openFileList;
    @FXML
    private TextField maxSizeInput;
    @FXML
    private Button processBtn;
    @FXML
    private ProgressBar pb;
    @FXML
    private ListView processDoneFileList;

    private List<File> fileList = new ArrayList<>();

    private double processValue = 0;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        openFileList.setPlaceholder(new Label("没有数据"));
        openFileBtn.setOnAction((ActionEvent t) -> {
            FileChooser chooser = new FileChooser(); // 创建一个文件对话框
            chooser.setTitle("打开文件"); // 设置文件对话框的标题
            chooser.setInitialDirectory(new File("E:\\")); // 设置文件对话框的初始目录
            // 给文件对话框添加多个文件类型的过滤器
            chooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("所有文件", "*.*"),
                    new FileChooser.ExtensionFilter("所有图片", "*.jpg", "*.gif", "*.bmp", "*.png"));
            // 显示文件打开对话框，且该对话框支持同时选择多个文件
            final List<File> files = chooser.showOpenMultipleDialog(primaryStage);// 显示文件打开对话框
            ObservableList<String> list = FXCollections.observableArrayList();
            fileList.clear();
            fileList.addAll(files);
            files.forEach(item -> {
                list.add(item.getName());
            });
            openFileList.setItems(list);
        });

        processBtn.setOnAction((ActionEvent t) -> {
            processDoneFileList.getItems().clear();
            final CharSequence characters = maxSizeInput.getCharacters();
            int s = fileList.size();
            AtomicInteger i = new AtomicInteger();
            fileList.forEach(item -> {
                processDoneFileList.getItems().add(item.getName());
                processDoneFileList.getOnEditCommit();
                final int i1 = i.getAndIncrement() % s;
            });


        });


    }
}
